<?php
function com_install() {
	global $database, $mosConfig_absolute_path, $mosConfig_mailfrom, $my;

	$now = time();
	# add indexes
	/*	$database->setQuery("ALTER TABLE #__yarbb_online ADD INDEX #__yarbb_online_user_id_idx(user_id)");
	$dbresult = $database->query();
	$database->setQuery("ALTER TABLE #__yarbb_posts ADD INDEX #__yarbb_posts_topic_id_idx(topic_id)");
	$dbresult = $database->query();
	$database->setQuery("ALTER TABLE #__yarbb_posts ADD INDEX #__yarbb_posts_multi_idx(poster_id, topic_id)");
	$dbresult = $database->query();
	$database->setQuery("ALTER TABLE #__yarbb_topics ADD INDEX #__yarbb_topics_forum_id_idx(forum_id)");
	$dbresult = $database->query();
	$database->setQuery("ALTER TABLE #__yarbb_topics ADD INDEX #__yarbb_topics_moved_to_idx(moved_to)");
	$dbresult = $database->query();
	$database->setQuery("ALTER TABLE #__yarbb_users ADD INDEX #__yarbb_users_registered_idx(registered)");
	$dbresult = $database->query();
	$database->setQuery("ALTER TABLE #__yarbb_users ADD INDEX #__yarbb_users_username_idx(username(8))");
	$dbresult = $database->query();*/
	//Добавляем группы
	$database->setQuery("SELECT COUNT(*) FROM #__forum_groups");
	$count = intval( $database->loadResult() );
	if ($count < 1) {
		$database->setQuery("INSERT INTO #__forum_groups (g_title, g_user_title, g_read_board, g_post_replies, g_post_topics, g_edit_posts, g_delete_posts, g_delete_topics, g_set_title, g_search, g_search_users, g_edit_subjects_interval, g_post_flood, g_search_flood) VALUES('Administrators', 'Администратор', 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0)");
		$dbresult = $database->query();
		$database->setQuery("INSERT INTO #__forum_groups (g_title, g_user_title, g_read_board, g_post_replies, g_post_topics, g_edit_posts, g_delete_posts, g_delete_topics, g_set_title, g_search, g_search_users, g_edit_subjects_interval, g_post_flood, g_search_flood) VALUES('Moderators', 'Модератор', 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0)");
		$dbresult = $database->query();
		$database->setQuery("INSERT INTO #__forum_groups (g_title, g_user_title, g_read_board, g_post_replies, g_post_topics, g_edit_posts, g_delete_posts, g_delete_topics, g_set_title, g_search, g_search_users, g_edit_subjects_interval, g_post_flood, g_search_flood) VALUES('Guest', 'Гость', 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0)");
		$dbresult = $database->query();
		$database->setQuery("INSERT INTO #__forum_groups (g_title, g_user_title, g_read_board, g_post_replies, g_post_topics, g_edit_posts, g_delete_posts, g_delete_topics, g_set_title, g_search, g_search_users, g_edit_subjects_interval, g_post_flood, g_search_flood) VALUES('Members', 'Пользователь', 1, 1, 1, 1, 1, 1, 0, 1, 1, 300, 60, 30)");
		$dbresult = $database->query();
	}

	// Добавляем ранги
	$database->setQuery("SELECT COUNT(*) FROM #__forum_ranks");
	$count = intval( $database->loadResult() );
	if ($count < 1) {
		$database->setQuery("INSERT INTO #__forum_ranks (rank, min_posts) VALUES('New member', 0)");
		$dbresult = $database->query();
		$database->setQuery("INSERT INTO #__forum_ranks (rank, min_posts) VALUES('Member', 10)");
		$dbresult = $database->query();
	}

	// Добавляем смайлы
	$database->setQuery("SELECT COUNT(*) FROM #__forum_smilies");
	$count = intval( $database->loadResult() );
	if ($count < 1) {
		$database->setQuery("INSERT INTO #__forum_smilies (Id, image, text) VALUES (1, 'smile.gif', ':)'), (2, 'sad.gif', ':('), (3, 'bo.gif', ':green:'), (4, 'heart.gif', ':heart:'), (5, 'angel.gif', ':ang:'), (6, 'blum.gif', ':-D'), (7, 'wink.gif', ';-D'), (8, 'dirol.gif', 'B-)'), (9, 'diablo.gif', ':diablo:)'), (10, 'rolleyes.gif', ':-S'), (11, 'blush.gif', ':-/');");
		$dbresult = $database->query();
	}

	// импортируем юзеров
	$database->setQuery("SELECT COUNT(*) FROM #__forum_users");
	$count = intval( $database->loadResult() );
	if ($count < 1) {
		# создание аккунта гостя
		$database->setQuery("INSERT INTO #__forum_users (group_id, username) VALUES(3, 'Гость')");
		$dbresult = $database->query();

		# импорт юзеров joostina
		$database->setQuery("SELECT * FROM #__users");
		$rows = $database->loadObjectList();
		for ($i = 0, $n = count($rows); $i < $n; $i++) {
			$row =& $rows[$i];
			if ($row->username == $my->username)
				$database->setQuery("INSERT INTO #__forum_users (id, group_id, jos_id, username, num_posts, last_post, last_visit) VALUES('".$row->id."', 1, '".$row->id."', '".$row->username."', 1, ".$now.",  UNIX_TIMESTAMP('".$row->lastvisitDate."'))");
			else
				$database->setQuery("INSERT INTO #__forum_users (id, group_id, jos_id, username, num_posts, last_post, last_visit) VALUES('".$row->id."', 4, '".$row->id."', '".$row->username."', 0, 0, UNIX_TIMESTAMP('".$row->lastvisitDate."'))");
			$dbresult = $database->query();
		}
	}

	// создаем конфиги в бд
	$yarbb_version = '1.0';
	$config = array(
			'o_cur_version'			=> '1.0',
			'o_board_title'			=> 'YARBB Forum',
			'o_board_desc' 			=> 'Лучший форум для Joostina',
			'o_time_format' 		=> 'H:i:s',
			'o_date_format' 		=> 'd-m-Y',
			'o_timeout_visit' 		=> '600',
			'o_timeout_online' 		=> '300',
			'o_redirect_delay' 		=> '1',
			'o_show_version' 		=> '1',
			'o_show_user_info' 		=> '1',
			'o_show_post_count'		=> '1',
			'o_smilies' 			=> '1',
			'o_smilies_sig' 		=> '1',
			'o_make_links'			=> '1',
			'o_default_style' 		=> 'newline',
			'o_default_user_group'  => '4',
			'o_topic_review' 		=> '15',
			'o_disp_topics_default' => '30',
			'o_disp_posts_default'  => '25',
			'o_indent_num_spaces' 	=> '4',
			'o_quickpost' 			=> '1',
			'o_users_online'		=> '1',
			'o_censoring' 			=> '1',
			'o_ranks' 				=> '1',
			'o_show_dot'			=> '0',
			'o_quickjump' 			=> '1',
			'o_gzip' 				=> '0',
			'o_additional_navlinks' => '',
			'o_report_method' 		=> '1',
			'o_avatars' 			=> '1',
			'o_search_all_forums' 	=> '1',
			'o_pms_enabled' 		=> '1',
			'o_pms_mess_per_page' 	=> '25',
			'o_admin_email' 		=> '',
			'o_webmaster_email' 	=> '',
			'o_subscriptions' 		=> '1',
			'o_smtp_host' 			=> NULL,
			'o_smtp_user' 			=> NULL,
			'o_smtp_pass' 			=> NULL,
			'o_announcement' 		=> '0',
			'o_announcement_message' => 'Нет анонсов.',
			'o_rules' 				=> '1',
			'o_rules_message' 		=> 'Нет правил.',
			'o_maintenance' 		=> '0',
			'o_maintenance_message' => 'Форум находится на обслуживании. Пожалуйста попробуйте еще раз через несколько минут.<br /><br />/Администрация',
			'p_mod_edit_users' 		=> '1',
			'p_mod_rename_users' 	=> '0',
			'p_mod_change_passwords' => '0',
			'p_mod_ban_users' 		=> '0',
			'p_message_bbcode' 		=> '1',
			'p_message_img_tag' 	=> '1',
			'p_message_all_caps' 	=> '1',
			'p_subject_all_caps' 	=> '1',
			'p_sig_all_caps' 		=> '0',
			'p_sig_bbcode' 			=> '1',
			'p_sig_img_tag' 		=> '1',
			'p_sig_length' 			=> '400',
			'p_sig_lines' 			=> '4',
			'p_allow_banned_email'  => '1',
			'p_allow_dupe_email' 	=> '0',
			'p_force_guest_email' 	=> '1',
			'o_image_formats' 		=> 'gif,png,jpg,jpeg',
			'o_new_window_link' 	=> '1',
			'o_board_logo' 			=> 'yarbb_logo_sm.png',
			'o_image_maxbytes' 		=> '102400',
			'o_image_maxdimensions' => '640x480',
			'o_guest_post_captchabox' => '1',
			'o_board_redirect' 		=> '',
			'o_board_redirect_url'  => NULL,
			'o_uploadile_thumb_size' => '100',
			'o_uploadile_limit_member' => '2097152',
			'o_uploadile_limit_modo' => '5242880',
			'o_uploadile_maxsize_member' => '1258291',
			'o_reputation_enabled'  => '1',
			'o_reputation_timeout'  => '300',
			'o_um_key' 				=> '',
			'o_um_default_zoom' 	=> '12',
			'o_um_default_lat' 		=> '48.857',
			'o_um_default_lng' 		=> '2.341',
			'o_merge_timeout' 		=> '60',
			'o_social_bookmarks' 	=> '1',
			'o_top_post_count' 		=> '10',
			'o_top_ten' 			=> '1',
			'o_max_users_online'    => '0',
			'o_rss_message_char' 	=> '100',
			'o_pms_uddeim' 			=> '0',
			'o_uploadile_thumb' 	=> '1',
			'o_uploadile_laws' 		=> 'zip,rar,jpg',
			'o_uploadile_maxsize_modo' => '1258291',
			'o_topic_labels' 		=> '1'
	);

	while (list($conf_name, $conf_value) = @each($config)) {
		$database->setQuery("SELECT COUNT(*) FROM #__forum_config WHERE conf_name='$conf_name'");
		$count = intval( $database->loadResult() );
		if ($count < 1) {
			$database->setQuery("INSERT INTO #__forum_config (conf_name, conf_value) VALUES('$conf_name', '$conf_value')");
			$dbresult = $database->query();
		} else if ($conf_name == "o_cur_version") {
			$database->setQuery("UPDATE #__forum_config SET conf_value=$conf_value WHERE conf_name='$conf_name'");
			$dbresult = $database->query();
		}
	}

	// insert some default data in an empty database...
	$database->setQuery("SELECT COUNT(*) FROM #__forum_categories");
	$count = intval( $database->loadResult() );
	if ($count < 1) {
		$database->setQuery("INSERT INTO #__forum_categories (cat_name, disp_position) VALUES('Тестовая категория', 1)");
		$dbresult = $database->query();
		$database->setQuery("INSERT INTO #__forum_forums (forum_name, forum_desc, num_topics, num_posts, last_post, last_post_id, last_poster, disp_position, cat_id, parent_forum_id, published) VALUES('Тестовый форум', 'Это - только испытательный форум', 1, 1, ".$now.", 1, '".$my->username."', 1, 1, 0, 1)");
		$dbresult = $database->query();
		$database->setQuery("INSERT INTO #__forum_topics (poster, subject, posted, last_post, last_post_id, last_poster, forum_id) VALUES('".$my->username."', 'Тестовое сообщение', ".$now.", ".$now.", 1, '".$my->username."', 1)");
		$dbresult = $database->query();
		$database->setQuery("INSERT INTO #__forum_posts (poster, poster_id, poster_ip, message, posted, topic_id) VALUES('".$my->username."', 62, '127.0.0.1', 'Форум успешно установлен! Также рекомендуем посетить другие наши ресурсы: www.yar-it.com  ', ".$now.', 1)');
		$dbresult = $database->query();
	}
	$database->setQuery("SELECT id FROM #__components WHERE admin_menu_link = 'option=com_forum'");
	$id = $database->loadResult();
	//add new admin menu images
	$database->setQuery("UPDATE #__components " . "SET admin_menu_img  = '../administrator/components/com_forum/images/yarbb_menu.png'" . ",   admin_menu_link = 'option=com_forum' " . "WHERE id='$id'");
	$database->query();
	# Show installation result to user
	?>
<style type="text/css">
	<!--
	.style1 {color: #990000}
	-->
</style>

<table width="65%" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
		<td width="20%">&nbsp;</td>
		<td colspan="2"><div class="moduletable">
				<form id=pay name=pay method="POST" action="https://merchant.webmoney.ru/lmi/payment.asp">
					<table width="99%" align="center" cellpadding="0" cellspacing="0" border="0">
						<tr>
							<td width="100%" align="center">

								<img src="http://yar-it.com/modules/logowm.gif" alt="Поблагодарить автора" title="Поблагодарить автора" /><br/>Поблагодарить автора		</td>
						</tr>
						<tr>
							<td width="100%" align="center">
								<input name="LMI_PAYMENT_AMOUNT" type="text" size="3" value="1">
								<input type="hidden" name="LMI_PAYMENT_DESC" value="Blagodarnost avtoru">
								<input type="hidden" name="LMI_PAYMENT_NO" value="1">
								<input type="hidden" name="LMI_SIM_MODE" value="0">

								<input type="hidden" name="LMI_SUCCESS_URL" value="http://yar-it.com/">
								<input type="hidden" name="LMI_SUCCESS_METHOD" value="2">
								<input type="hidden" name="LMI_FAIL_URL" value="http://yar-it.com/">
								<input type="hidden" name="LMI_FAIL_METHOD" value="2">
								<select name="LMI_PAYEE_PURSE" style="min-width:30px;">
									<option value="Z222701768194">WMZ</option>
									<option value="E974972909428">WME</option>
									<option value="R332765512780">WMR</option>
								</select>		</td>
						</tr>
						<tr>
							<td width="100%" align="center">
								<br>
								<input type="submit" class="button" value="Отправить">		</td>
						</tr>
					</table>
				</form>		</div></td>
		<td width="21%">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2" valign="top"></td>
		<td colspan="2" valign="top"></td>
	</tr>
	<tr>
		<td colspan="4"><div align="center">
				<p><strong>Форум моральной поддержки <a href="http://yar-it.com">http://yar-it.com</a><br /></strong></p>
			</div></td>
	</tr>

</table>
	<?php
}